﻿using System.IO;
using System.Net;
using System.Text;

namespace SVTTranslateTextEngine.Core
{
    /// <summary>
    /// This class implements a correlation method based on the Http way the service request
    /// </summary>
    public class HttpModelHelper
    {
        /// <summary>
        /// Request to the google translate engine by the POST method
        /// </summary>
        /// <param name="szUrl">the request URL string </param>
        /// <returns>return the text stream, can be interpreted as a JSON object </returns>
        internal string GetResultStringByRequestPostForGoogle(string szUrl)
        {
            var innerRequest = (HttpWebRequest) WebRequest.Create(szUrl);
            innerRequest.Headers.Add("X-HTTP-Method-Override", "POST");
            var innerResponse = (HttpWebResponse) innerRequest.GetResponse();
            Stream rStream = innerResponse.GetResponseStream();
            if (rStream == null) return null;
            var respStream = new StreamReader(rStream, Encoding.UTF8);
            string resultString;
            try
            {
                resultString = respStream.ReadToEnd();
            }
            finally
            {
                respStream.Dispose();
                respStream.Close();
                innerResponse.Close();
            }
            return resultString;
        }
    }
}